﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using Croxweb.Leuphana.Services.StorageService;

namespace Croxweb.Leuphana.Services.StorageService
{
    public class PhoneSettingStore : ISettingStore
    {
        private readonly IsolatedStorageSettings storage;

        public PhoneSettingStore()
        {
            this.storage = IsolatedStorageSettings.ApplicationSettings;
        }

        public String Username
        {
            get { return getValueOrDefault<String>("username", ""); }
            set { addOrUpdateValue("username", value); }
        }

        public String Password
        {
            get { return getValueOrDefault<String>("password", ""); }
            set { addOrUpdateValue("password", value); }
        }

        public String Semester
        {
            get { return getValueOrDefault<String>("semester", ""); }
            set { addOrUpdateValue("semester", value); }
        }

        public Boolean ValidatedUser
        {
            get { return getValueOrDefault<Boolean>("validatedUser", false); }
            set { addOrUpdateValue("validatedUser", value); }
        }

        public DateTime LastMessageSync
        {
            get { return getValueOrDefault<DateTime>("lastMessageSync", DateTime.MinValue); }
            set { addOrUpdateValue("lastMessageSync", value); }
        }

        public DateTime LastScheduleSync
        {
            get { return getValueOrDefault<DateTime>("lastScheduleSync", DateTime.MinValue); }
            set { addOrUpdateValue("lastScheduleSync", value); }
        }

        #region Isolated Storage Access

        private void addOrUpdateValue(String key, object value)
        {
            bool valueChanged = false;

            try
            {
                // If new value is different, set the new value.
                if (this.storage[key] != value)
                {
                    this.storage[key] = value;
                    valueChanged = true;
                }
            }
            catch (KeyNotFoundException)
            {
                this.storage.Add(key, value);
                valueChanged = true;
            }
            catch (ArgumentException)
            {
                this.storage.Add(key, value);
                valueChanged = true;
            }

            if (valueChanged)
            {
                this.save();
            }
        }

        private T getValueOrDefault<T>(String key, T defaultValue)
        {
            T value;

            try
            {
                value = (T)this.storage[key];
            }
            catch (KeyNotFoundException)
            {
                value = defaultValue;
            }
            catch (ArgumentException)
            {
                value = defaultValue;
            }

            return value;
        }

        private void save()
        {
            this.storage.Save();
        }

        #endregion
    }
}
